﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Liya.Crm.IService;
using Liya.Crm.Data.Models;
using Liya.Crm.Data;

namespace Liya.Crm.Service
{
    public class Function : IFunction
    {
        //public IList<Sys_Function> GetFunctions(Sys_UserInfo userInfo)
        //{
        //    using (Ly_CrmContext context = new Ly_CrmContext())
        //    {
        //        var result = (from s in context.Sys_Function
        //                     join p in context.Sys_Permission on s.FId equals p.PFunctionId
        //                     join r in context.Sys_Role on p.PRoleId equals r.RId
        //                     join ru in context.Sys_RoleUser on r.RId equals ru.RURoleId
        //                     where ru.RUUserId.Equals(userInfo.UId)
        //                     select s).ToList();
        //        return result;
        //    }
        //}

        /// <summary>
        /// 根据角色获取用户权限
        /// </summary>
        /// <param name="roleIds"></param>
        /// <returns></returns>
        public IList<Sys_Function> GetFunctionsByRoles(IList<int> roleIds)
        {
            using (Ly_CrmContext context = new Ly_CrmContext())
            {
                var result = (from s in context.Sys_Function
                              join p in context.Sys_Permission on s.FId equals p.PFunctionId
                              join r in context.Sys_Role on p.PRoleId equals r.RId
                              where roleIds.Contains(r.RId)
                              select s).ToList();
                return result;
            }
        }

        public IEnumerable<Sys_Function> GetFunctionsByParent(int parentId)
        {
            using (Ly_CrmContext context = new Ly_CrmContext())
            {
                return  (from s in context.Sys_Function
                             where s.FParentId == parentId
                             select s);
            }
        }
    }
}
